********************************************************************************
** 	TITLE:		UK64_results                                                  ** 
**	AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       1918-2019election_results_by_pcon         		              **
**	VERSION:	Stata 16					                                  **
**	DATE:		October 2022  				                                  **
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close
log using "UK64_results.smcl", replace

* Import the dataset

import excel "1918-2019election_results_by_pcon.xlsx", cellrange(B5:X634) sheet(1964) clear

* Reshape

drop G J M P S V

rename B id
rename C constituency_name
rename D county
rename E region
rename F electorate
rename H votes1
rename I share1
rename K votes2
rename L share2
rename N votes3
rename O share3
rename Q votes4
rename R share4
rename T votes5
rename U share5
rename W totalvotes
rename X turnout

reshape long votes share, i(id) j(party_code)  

gen party_name = " "

replace party_name = "Conservative" if party_code == 1 
replace party_name = "Labour" if party_code == 2
replace party_name = "Liberal" if party_code == 3
replace party_name = "Nationalists" if party_code == 4
replace party_name = "Other" if party_code == 5

drop party_code

gen party_code = .

replace party_code = 1 if party_name == "Conservative"
replace party_code = 2 if party_name == "Labour"
replace party_code = 3 if party_name == "Liberal"
replace party_code = 4 if party_name == "Nationalists"
replace party_code = 5 if party_name == "Other"
replace party_code = -9999 if region == "Northern Ireland"
 
* Generate rank of parties

gsort constituency_name -votes
bysort constituency_name : gen rank = _n

gen first = party_name if rank == 1
gen second = party_name if rank == 2
gen third = party_name if rank == 3

bysort constituency_name : gen winner64 = first[1]
bysort constituency_name : gen runnerup64 = second[2]
bysort constituency_name : gen thirdplace64 = third[3]

* Generate margins of victory

bysort constituency_name : gen margin = (share[1] - share[2])*100

* Generate effective number of electoral parties

gen sqrvotepr = (share)^2

bysort constituency_name : egen sumsqrvotepr = sum(sqrvotepr)

gen enep = 1/sumsqrvotepr

* Labelling parties

replace party_name = "1" if party_name == "Conservative"
replace party_name = "2" if party_name == "Labour"
replace party_name = "3" if party_name == "Liberal"
replace party_name = "4" if party_name == "Nationalists"
replace party_name = "5" if party_name == "Other"

destring (party_name), replace

replace winner64 = "1" if winner64 == "Conservative"
replace winner64 = "2" if winner64 == "Labour"
replace winner64 = "3" if winner64 == "Liberal"
replace winner64 = "4" if winner64 == "Nationalists"
replace winner64 = "5" if winner64 == "Other"

destring (winner64), replace

replace runnerup64 = "1" if runnerup64 == "Conservative"
replace runnerup64 = "2" if runnerup64 == "Labour"
replace runnerup64 = "3" if runnerup64 == "Liberal"
replace runnerup64 = "4" if runnerup64 == "Nationalists"
replace runnerup64 = "5" if runnerup64 == "Other"

destring (runnerup64), replace

replace thirdplace64 = "1" if thirdplace64 == "Conservative"
replace thirdplace64 = "2" if thirdplace64 == "Labour"
replace thirdplace64 = "3" if thirdplace64 == "Liberal"
replace thirdplace64 = "4" if thirdplace64 == "Nationalists"
replace thirdplace64 = "5" if thirdplace64 == "Other"

destring (thirdplace64), replace

label define party 1 "Conservative" 2 "Labour" 3 "Liberal" 4 "Nationalists" 5 "Other"
label values party_name winner64 runnerup64 thirdplace64 party
 
* Selecting, ordering, and sorting variables

keep id constituency_name party_name rank votes share margin enep winner64 runnerup64 thirdplace64

order id constituency_name party_name rank votes share margin enep winner64 runnerup64 thirdplace64

sort id rank

drop if rank > 3

* Clean district names

replace constituency_name = upper(constituency_name)

* Drop duplicates

duplicates tag id, gen(dup)
duplicates drop id, force
drop dup

* Create election variable

gen election64 = "1964 British general election"

* Saving file

save UK64_results, replace

* Merge with UK59_results.dta and generate reelection variable

merge 1:1 _n using UK59_results.dta

* Generate variable for incumbent party reelection

gen reelected64 = 1 if winner64 == winner59
replace reelected64 = 0 if reelected64!=1

* Saving file

save UK5964_results, replace

log close